Refresh.Web.PopupWindow = {
	_currentUrl: null,
	_currentWindow: null,
	
	open: function(url, width, height) {
		/*
		if (this._currentWindow != null || !this._currentWindow.closed) {
			if (url == this._currentUrl) { 
				this._currentWindow.focus();
				return;
			} else {
				this._currentWindow.focus();
			}	
		}
		*/
		
		this._currentUrl = url;
		this._currentWindow = window.open(this._currentUrl, 'ftbwindow','height=' + height + ', width=' + width + ', toolbars=0, menubar=0, resizable=1');
	}
};

Refresh.Web.ModalWindow = {
	_initialized : false,
	
	_loadingUrl: Refresh.windowPath + 'blank.html',
	
	initialize: function() {
		
		// overlay to stop clicks
		this._overlay = document.createElement('div');
		this._overlay.id = 'ModalOverlay';
		document.body.appendChild(this._overlay);
		
		this._overlay.className = 'modalOverlay';
		
		// main container
		this._container = document.createElement('div');
		this._container.id = 'ModalContainer';
		document.body.appendChild(this._container);
		
		this._container.innerHTML = '<div id="ModalInner">' + 
			'<div id="ModalTitleBar">' + 
				'<div id="ModalTitle"></div>' + 
				'<div id="ModalButtons">' + 
					'<div id="ModalCloseButton" onclick="Refresh.Web.ModalWindow.hide();" ></div>' + 
				'</div>' + 
			'</div>' + 
				'<iframe src="' + this._loadingUrl + '" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="ModalFrame" name="ModalFrame" width="100%" height="100%"></iframe>' + 
		'</div>';
		
		this._frame = $('ModalFrame');
		this._titleBar = $('ModalTitleBar');
		
		Event.observe(this._titleBar, "mousedown", this._initDrag.bindAsEventListener(this));
		
		this._hideSelects = true;
		this._hideObjects= true;
		
		this._initialized = true;
	},
	
	show: function(url, width, height) {
		if (Refresh.Web.BrowserInfo.isSafari) {
			Refresh.Web.PopupWindow.open(url, width, height);
			return;
		}
		
		if (!this._initialized)
			this.initialize();
			
		this._overlay.style.display = "block";
		this._container.style.display = "block";
		// calculate where to place the window on screen
		
		this._centerModalWindow(width, height);
		
		var titleBarHeight = parseInt(document.getElementById("ModalTitleBar").offsetHeight, 10);
		
		this._container.style.width = width + "px";
		this._container.style.height = (height+titleBarHeight) + "px";
		
		// need to set the width of the iframe to the title bar width because of the dropshadow
		// some oddness was occuring and causing the frame to poke outside the border in IE6
		this._frame.style.width = parseInt(document.getElementById("ModalTitleBar").offsetWidth, 10) + "px";
		this._frame.style.height = (height) + "px";
		
		// store the  title for later loading
		this._tempTitle = window.frames["ModalFrame"].document.title;
		
		// set the url
		this._frame.src = url;
		
		//gReturnFunc = returnFunc;
		
		// for IE
		if (Refresh.Web.BrowserInfo.isIE) {
			this._hideSelectBoxes();
		}
		
		
		
		window.setTimeout("Refresh.Web.ModalWindow.setPopTitle();", 249);		
	},
	
	hide: function() {
		if (!this._initialized)
			return;
			
		this._overlay.style.display = "none";
		this._container.style.display = "none";
		
		//if (callReturnFunc == true && gReturnFunc != null) {
		//	gReturnFunc(window.frames["popupFrame"].returnVal);
		//}
		
		this._frame.src = this._loadingUrl;
		
		// display all select boxes
		if (this._hideSelects || this._hideObjects) {
			this._displaySelectBoxes();
		}
	},
	
	_centerModalWindow: function(width, height) {
		var fullHeight = this.getViewportHeight();
		var fullWidth = this.getViewportWidth();
		
		var theBody = document.documentElement;
		
		var scTop = parseInt(theBody.scrollTop,10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		
		this._overlay.style.height = fullHeight + "px";
		this._overlay.style.width = fullWidth + "px";
		this._overlay.style.top = scTop + "px";
		this._overlay.style.left = scLeft + "px";
				
		var titleBarHeight = parseInt(document.getElementById("ModalTitleBar").offsetHeight, 10);
		
		this._container.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
		this._container.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";	
	},
	
	getViewportHeight: function() {
		if (window.innerHeight!=window.undefined) return window.innerHeight;
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
		if (document.body) return document.body.clientHeight; 
		return window.undefined; 
	},
	
	getViewportWidth: function() {
		if (window.innerWidth!=window.undefined) return window.innerWidth; 
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
		if (document.body) return document.body.clientWidth; 
		return window.undefined; 
	},
	
	_hideSelectBoxes: function() {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if( (this._hideSelects && document.forms[i].elements[e].tagName == "SELECT") 
					|| (this._hideObjects && document.forms[i].elements[e].tagName == "OBJECT") ) {
					document.forms[i].elements[e].style.visibility="hidden";
				}
			}
		}
	},

	_displaySelectBoxes: function() {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if( (this._hideSelects && document.forms[i].elements[e].tagName == "SELECT") 
					|| (this._hideObjects && document.forms[i].elements[e].tagName == "OBJECT") ) {
					document.forms[i].elements[e].style.visibility="visible";
				}
			}
		}
	},
	
	_initDrag: function() {
	
	},
	
	setPopTitle: function() {
		if (window.frames["ModalFrame"].document.title == null || window.frames["ModalFrame"].document.title == this._tempTitle)  {
			window.setTimeout("Refresh.Web.ModalWindow.setPopTitle();", 99);
		} else {
			document.getElementById("ModalTitle").innerHTML = window.frames["ModalFrame"].document.title;
		}
	}
};